package com.travelsky.airchina.boarding.dao.impl.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.travelsky.airchina.boarding.common.ConstType;
import com.travelsky.airchina.boarding.dao.NewsDAO;
import com.travelsky.airchina.boarding.domain.News;
import com.travelsky.airchina.boarding.dto.NewsSearchCondition;
import com.travelsky.airchina.boarding.dto.NewsView;
import com.travelsky.airchina.boarding.util.PageUtil;

@Repository("newsDAO")
public class NewsDAOImpl extends GenericDAOHibernate<News,Long,NewsDAO> implements NewsDAO {
	@Autowired
	public NewsDAOImpl(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findNewsPage(PageUtil pageUtil, NewsSearchCondition newsSearchCondition) {
		StringBuffer countQuery = new StringBuffer("select count(*) from News news_ where 1=1 ");
		StringBuffer query = new StringBuffer("select news_.id,news_.title,news_.newsColumn.name,news_.status,news_.createdTime,news_.htmlUrl from News news_ where 1=1 ");
		Map<String,Object> params = new HashMap<String,Object>();
		if(newsSearchCondition!=null){
			if(newsSearchCondition.getColumnIds()!=null && newsSearchCondition.getColumnIds().size()>0){
				countQuery.append(" and news_.newsColumn.id in (:newsColumnId)");
				query.append(" and news_.newsColumn.id in (:newsColumnId)");
				params.put("newsColumnId", newsSearchCondition.getColumnIds());
			}
			if(newsSearchCondition.getTitle()!=null && !"".equals(newsSearchCondition.getTitle().trim())){
				countQuery.append(" and news_.title like :newsTitle");
				query.append(" and news_.title like :newsTitle");
				params.put("newsTitle", "%"+newsSearchCondition.getTitle().trim()+"%");
			}
			if(newsSearchCondition.getBeginTime()!=null && newsSearchCondition.getEndTime()!=null){
				countQuery.append(" and news_.createdTime >= :getBeginTime and news_.createdTime <= :getEndTime");
				query.append(" and news_.createdTime >= :getBeginTime and news_.createdTime <= :getEndTime");
				params.put("getBeginTime", newsSearchCondition.getBeginTime());
				params.put("getEndTime", newsSearchCondition.getEndTime());
			}else if(newsSearchCondition.getBeginTime()!=null){
				countQuery.append(" and news_.createdTime >= :createdTime");
				query.append(" and news_.createdTime >= :createdTime");
				params.put("createdTime", newsSearchCondition.getBeginTime());
			}else if(newsSearchCondition.getEndTime()!=null){
				countQuery.append(" and news_.createdTime <= :createdTime");
				query.append(" and news_.createdTime <= :createdTime");
				params.put("createdTime", newsSearchCondition.getEndTime());
			}
			if(newsSearchCondition.getNewStatus()!=null && !"".equals(newsSearchCondition.getNewStatus())){
				countQuery.append(" and news_.status = :getNewStatus");
				query.append(" and news_.status = :getNewStatus");
				params.put("getNewStatus", newsSearchCondition.getNewStatus());
			}
			if(newsSearchCondition.getColumnType()!=null && !"".equals(newsSearchCondition.getColumnType())){
				countQuery.append(" and news_.newsColumn.columnType = :getColumnType");
				query.append(" and news_.newsColumn.columnType = :getColumnType");
				params.put("getColumnType", newsSearchCondition.getColumnType());
			}
			/**
			if(newsSearchCondition.getIsVideo()!=null){
				countQuery.append(" and news_.isVideo = :isVideo");
				query.append(" and news_.isVideo = :isVideo");
				params.put("isVideo", newsSearchCondition.getIsVideo());
			}else{
				countQuery.append(" and ( news_.isVideo = '0' or news_.isVideo is null) ");
				query.append(" and ( news_.isVideo = '0' or news_.isVideo is null) ");
			}*/
		}
		query.append(" order by news_.createdTime desc,news_.status");
		List<Object[]> listTemp = this.findQueryList(pageUtil, countQuery.toString(), query.toString(), params);
		if(listTemp!=null && listTemp.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] objs:listTemp){
				NewsView o = new NewsView();
				o.setId((Long)objs[0]);
				o.setTitle((String)objs[1]);
				o.setColumnName((String)objs[2]);
				o.setStatus((String)objs[3]);
				o.setCreatedTime((Date)objs[4]);
				o.setHtmlUrl((String)objs[5]);
				list.add(o);
			}
			params = null;
			return list;
		}
		params = null;
		return null;
	}
	public int getNewsSum() {
		String hql = "select count(id) from News ";
		int sum = this.countHqlQuery(hql, null);
		return sum;
	}
	public boolean updateNewsStatus(Long id, String status) {
		String hql = "update News set status = ? where id = ?";
		int updateSum = this.getHibernateTemplate().bulkUpdate(hql,status,id);
		if(updateSum>0){
			return true;
		}else{
			return false;
		}
	}
	@SuppressWarnings("unchecked")
	public List<News> getNewsListByNewsColumnCode(String newsColumnCode) {
		String hql = "from News where newsColumn.code = ? and status = ? order by createdTime desc ";
		return this.getHibernateTemplate().find(hql,newsColumnCode,ConstType.NEWS_STATUS_AUDIT_PUBLISH);
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findLastedNewsListByNewsColumnCode(
			String newsColumnCode, int sum) {
		String hql = "select news_.id,news_.title,news_.imageFile,news_.createdTime,news_.htmlUrl from News news_ where news_.newsColumn.code = :newsColumnCode and news_.status = :newsStatus order by news_.createdTime desc ";
		Query query = this.getSession().createQuery(hql);
		query.setParameter("newsColumnCode", newsColumnCode);
		query.setParameter("newsStatus", ConstType.NEWS_STATUS_AUDIT_PUBLISH);
		query.setFirstResult(0);
		query.setMaxResults(sum);
		List<Object[]> objs = query.list();
		if(objs!=null && objs.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] obj:objs){
				NewsView temp = new NewsView();
				temp.setId((Long)obj[0]);
				temp.setTitle((String)obj[1]);
				temp.setImageFile((String)obj[2]);
				temp.setCreatedTime((Date)obj[3]);
				temp.setHtmlUrl((String)obj[4]);
				list.add(temp);
			}
			return list;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findLastedVideoListByNewsColumnCode(
			String newsColumnCode, int sum) {
		String hql = "select news_.id,news_.name,news_.videoPath,news_.createdTime from Video news_ where news_.column.code = :newsColumnCode and news_.status = :newsStatus order by news_.createdTime desc ";
		Query query = this.getSession().createQuery(hql);
		query.setParameter("newsColumnCode", newsColumnCode);
		query.setParameter("newsStatus", ConstType.NEWS_STATUS_AUDIT_PUBLISH);
		query.setFirstResult(0);
		query.setMaxResults(sum);
		List<Object[]> objs = query.list();
		if(objs!=null && objs.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] obj:objs){
				NewsView temp = new NewsView();
				temp.setId((Long)obj[0]);
				temp.setTitle((String)obj[1]);
				temp.setVideoPath((String)obj[2]);
				temp.setCreatedTime((Date)obj[3]);
				list.add(temp);
			}
			return list;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findNewsPageByColumnId(PageUtil pageUtil,Long columnId) {
		String countQuery = "select count(news_.id) from News news_ where news_.newsColumn.id = :newsColumnId and news_.status = :newsStatus order by news_.createdTime desc ";
		String hql = "select news_.id,news_.title,news_.createdTime from News news_ where news_.newsColumn.id = :newsColumnId and news_.status = :newsStatus order by news_.createdTime desc ";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("newsColumnId", columnId);
		params.put("newsStatus", ConstType.NEWS_STATUS_AUDIT_PUBLISH);
		List<Object[]> listTemp = this.findQueryList(pageUtil, countQuery, hql, params);
		if(listTemp!=null && listTemp.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] obj:listTemp){
				NewsView temp = new NewsView();
				temp.setId((Long)obj[0]);
				temp.setTitle((String)obj[1]);
				temp.setCreatedTime((Date)obj[2]);
				list.add(temp);
			}
			return list;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findNewsPageByCondition(PageUtil pageUtil,NewsSearchCondition newsSearchCondition) {
		StringBuffer countQuery = new StringBuffer("select count(*) from News news_ where news_.status = :newsStatus ");
		StringBuffer query = new StringBuffer("select news_.id,news_.title,news_.createdTime,news_.htmlUrl from News news_ where news_.status = :newsStatus ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("newsStatus", ConstType.NEWS_STATUS_AUDIT_PUBLISH);
		if(newsSearchCondition!=null){
			if(newsSearchCondition.getTitle()!=null && !"".equals(newsSearchCondition.getTitle().trim())){
				countQuery.append(" and ( news_.title like :newsTitle or news_.content like :contentKey ) ");
				query.append(" and ( news_.title like :newsTitle or news_.content like :contentKey ) ");
				params.put("newsTitle", "%"+newsSearchCondition.getTitle().trim()+"%");
				params.put("contentKey", "%"+newsSearchCondition.getTitle().trim()+"%");
			}
		}
		List<Object[]> listTemp = this.findQueryList(pageUtil, countQuery.toString(), query.toString(), params);
		if(listTemp!=null && listTemp.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] obj:listTemp){
				NewsView temp = new NewsView();
				temp.setId((Long)obj[0]);
				temp.setTitle((String)obj[1]);
				temp.setCreatedTime((Date)obj[2]);
				temp.setHtmlUrl((String)obj[3]);
				list.add(temp);
			}
			params = null;
			return list;
		}
		params = null;
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findNewsPageByColumnCode(PageUtil pageUtil,String columnCode) {
		String countQuery = "select count(news_.id) from News news_ where news_.newsColumn.code = :columnCode and news_.status = :newsStatus order by news_.createdTime desc ";
		String hql = "select news_.id,news_.title,news_.createdTime from News news_ where news_.newsColumn.code = :columnCode and news_.status = :newsStatus order by news_.createdTime desc ";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("columnCode", columnCode);
		params.put("newsStatus", ConstType.NEWS_STATUS_AUDIT_PUBLISH);
		List<Object[]> listTemp = this.findQueryList(pageUtil, countQuery, hql, params);
		if(listTemp!=null && listTemp.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] obj:listTemp){
				NewsView temp = new NewsView();
				temp.setId((Long)obj[0]);
				temp.setTitle((String)obj[1]);
				temp.setCreatedTime((Date)obj[2]);
				list.add(temp);
			}
			params = null;
			return list;
		}
		params = null;
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findLastedVideoList(int sum) {
		String hql = "select news_.id,news_.title,news_.videoPath,news_.createdTime from News news_ where news_.isVideo = :isVideo and news_.status = :newsStatus order by news_.createdTime desc ";
		Query query = this.getSession().createQuery(hql);
		query.setParameter("newsStatus", ConstType.NEWS_STATUS_AUDIT_PUBLISH);
		query.setParameter("isVideo", "1");
		query.setFirstResult(0);
		query.setMaxResults(sum);
		List<Object[]> objs = query.list();
		if(objs!=null && objs.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] obj:objs){
				NewsView temp = new NewsView();
				temp.setId((Long)obj[0]);
				temp.setTitle((String)obj[1]);
				temp.setVideoPath((String)obj[2]);
				temp.setCreatedTime((Date)obj[3]);
				list.add(temp);
			}
			return list;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<NewsView> findPictureNewsList(int sum) {
		String hql = "select news_.id,news_.title,news_.imageFile,news_.createdTime,news_.htmlUrl from News news_ where news_.newsColumn.isImageText = :isImageText and news_.status = :newsStatus order by news_.createdTime desc ";
		Query query = this.getSession().createQuery(hql);
		query.setParameter("isImageText", "1");
		query.setParameter("newsStatus", ConstType.NEWS_STATUS_AUDIT_PUBLISH);
		query.setFirstResult(0);
		query.setMaxResults(sum);
		List<Object[]> objs = query.list();
		if(objs!=null && objs.size()>0){
			List<NewsView> list = new ArrayList<NewsView>();
			for(Object[] obj:objs){
				NewsView temp = new NewsView();
				temp.setId((Long)obj[0]);
				temp.setTitle((String)obj[1]);
				temp.setImageFile((String)obj[2]);
				temp.setCreatedTime((Date)obj[3]);
				temp.setHtmlUrl((String)obj[4]);
				list.add(temp);
			}
			return list;
		}
		return null;
	}

}
